# Starting PIC microcontroller programming – Part 1/2 (Setting up the tools)

08-29-2016 Alex Microcontrollers, Tutorials 0

In this series of tutorials we’re going to learn how to create our first embedded program for PIC microcontrollers. They are built by Microchip, and massively used in the industry. There are several other big actors in the world of microcontroller, such as Atmel with their AVR products (although they’ve recently been bought by Microchip), NXP, Freescale and much more. I decided to present Microchip’s microcontrollers because they are one of the most used brand of microcontrollers, and because the tools are easy to set up. They’re also relatively cheap and available on Windows, Mac and Linux, making it accessible to most people.

Enough talking, let’s start working! The absolute first step before programming PIC microcontrollers, will be to install the necessary tools on our computer. We’ll start by setting up MPLAB X and the compiler XC8 on our computer, then we’ll test that everything is running fine. After our tools are ready, be sure to read the second part of this tutorial to discover how to build our first circuit and program!

# Components needed

First we’ll need a computer, which should not be a problem if you’re reading this tutorial. You can use a Windows, MAC or Linux computer, as the software tools are available on all three operating systems.

We also need a PIC programmer, which is a hardware tool that we’ll use to write our program into the microcontroller (we say we “program” the microcontroller). I personally use a PICkit 3 from Microchip (about $50 on Newark), but you can use any programmer compatible with the PIC microcontrollers you want to program. The PICKit 3 is the best option in my opinion, as it allows in-circuit programming and debugging, which means you’ll be able to simply plug it in your circuit and reprogram it as many times as needed. Other programmers require you to remove the microcontroller from the circuit to reprogram it; I would recommend avoiding those.

There are also a lot of unofficial copies of the PICKit 3 (eBay, Amazon, etc.) that you can buy and try. Most of them will probably work, but I’m not sure about their reliability and don’t know if they implement the full features of the official one. The PICKit 3 may seem expensive, but it’s only a one time expense, after buying it you will not have any other purchase to make.

PicKit 3 programmer

# Installing MPLAB X

Let’s install MPLAB X. It’s the IDE (Integrated Development Environment) that we will use to write our code and transfer it into the PIC. MPLAB X is offered by Microchip and completely free to download and use. It’s made specifically for embedded development with PIC microcontrollers.

I will be using a computer running Mac OS X, but Microchip also offers versions for Windows and Linux. You can download it on Microchip’s website, it will be at the bottom of the page under the Download tab. Follow the steps given by the installer to install it.

Download MPLAB X

# Installing XC8

The IDE itself comes without any compiler. The compiler is the tool that will “translate” our source code into a binary code that the microcontroller will be able to understand and execute. Once MPLAB X is installed, go on this page and at the bottom, under the Download tab, download the compiler XC8 for your operating system. This is the most recent compiler offered by Microchip, and it is also the easiest to work with in my opinion (much more convenient than the previous one, C18). You’ll notice there are two other compilers named XC16 and X32. The number represents the number of bits of the microcontroller’s architecture. As PIC16 and PIC18 families are 8-bit architectures (8-bits data buses), we need XC8. If you want to work with PIC32 which is a 32-bit processor, you’ll need XC32.

Download XC8

And that’s it! MPLAB X and the compiler are the only software tools we need to program our PIC.

# Testing the tools

Let’s make sure everything was installed properly before programming anything. We’ll follow these steps:

- Open MPLAB X
- Connect the PicKit 3 with the USB cable
- The “status” LED color will indicate if it’s working properly. It should be green if there is no problem.

For some reason mine was red after the first connexion. To fix this, we will need to load the necessary firmware into the PICkit 3, and for this we have to create an empty project and act like if we were programming a PIC.

First, create and empty project by clicking on File => New Project. Select “Standalone Project” under the “Microchip Embedded” category.

Create a new project in MPLAB X

Then, select a PIC for this project. I selected a PIC16F88 under the “Baseline 8-bit MCUs (PIC10/12/16)” family. You can use any of the PIC16F or PIC18F microcontrollers, I used a PIC16F88 because that’s the one I mainly use when I program.

Select a device : PIC16F88

Next select the hardware tool you want to use to program the PIC. I choose the PICkit 3. If your programmer is connected to your computer, it will appear under its category. For example, my PICkit 3’s serial number appeared under PICkit 3. You should also have two green dots next to PICkit 3 to indicate that the tool is supported by your installation of MPLAB X.

Select a tool: PICkit 3

Select the compiler XC8. If you’ve correctly installed it, you should have two green dots next to XC8.

Select a compiler : XC8

The final step is to give a name to your project and select a location for it. Those values are up to you.

Choose a name and directory

Click on “Finish”. Our project is created, now we just need to add a main file to it. To do so, on the left part of the window, right click on Source Files, then select New => main.c.

Create a new main file

Give a name to the file, like “main.c” and click on “Finish”. The new file should open in the editor area.

We’ll keep the default empty main function, like shown below.

Empty main function
C
#include <xc.h> void main(void) { return; }

1 2 3 4 5 | #include <xc.h> void main(void) { return; }

Now click on Program Target Device to compile your code and program the microcontroller. You don’t need to have a microcontroller connected to the programmer, as this trick is only used to download the necessary firmware into the PicKit 3.

Program the PIC

If everything goes fine, you should see the following result on the PicKit 3 output, at the bottom of the IDE:

PicKit 3 output
Currently loaded firmware on PICkit 3 Firmware Suite Version.....01.32.09 * Firmware type..............dsPIC33F/24F/24H Now Downloading new Firmware for target device: PIC16F88 Downloading bootloader Bootloader download complete Programming download... Downloading RS... RS download complete Programming download... Downloading AP... AP download complete Programming download...

1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Currently loaded firmware on PICkit 3 Firmware Suite Version.....01.32.09 * Firmware type..............dsPIC33F/24F/24H Now Downloading new Firmware for target device: PIC16F88 Downloading bootloader Bootloader download complete Programming download... Downloading RS... RS download complete Programming download... Downloading AP... AP download complete Programming download...

You can cancel when a pop-up asks you if the selected device is the same as the attached device, as we don’t want to actually program a device.

After this, your status LED should shine green. Congratulations, you’re ready to program your first embedded application, just follow the second part of the tutorial!

LEDs for a successfully connected PicKit 3

### Share this:

- Email
- Twitter
- Facebook
- Google

### Like this:

Like Loading...

### Related

Starting PIC microcontroller programming - Part 2/2 (First project)09-01-2016 In "Microcontrollers"

How to use UART with a PIC microcontroller 08-06-2017 In "Microcontrollers"

How to handle interrupts on a PIC microcontroller 08-26-2017 In "Microcontrollers"

- microcontroller
- mplab x
- PIC16F88
- PICkit 3

#### Be the first to comment

You must log in to post a comment.

Copyright © The Embedded Lab 2016 - 2018 | The Embedded Lab is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn fees by linking to Amazon.com and affiliated sites.

%d bloggers like this:

Send to Email Address Your Name Your Email Address Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.
LEDs for a successfully connected PicKit 3 Image 12 of 12